<link href="css/bootstrap.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>

<div id="gioithieu">
	<div id="tieude_1">
		<p>Thêm trẻ mới</p>
	</div>
<div id="noidung_gt">
	<?php
		$result = $mysqli->query("select MAX(MA_TRE) as max_mt from tre");
		$row = $result->fetch_array();
		$matre = "T".str_replace(".","",number_format(((int) ltrim($row[0],"T") + 1)/1000,3));
	?>
    <form name="formnew" id="formnew" method="post">
    	<table border="0" id="bang" class="chu">
        	<tr>
            	<td width="30%" ><p>Mã trẻ</p></td>
                <td width="70%">
                    <input type="text" name="matre" id="matre" size="10" value="<?php echo $matre; ?>" readonly />
                </td>
            </tr>
            <tr>
            	<td><p>Họ tên trẻ <span>(*)</span></p></td>
                <td>
                    <input type="text" name="hotentre" id="hotentre" size="40" />
                </td>
            </tr>
            <tr>
            	<td ><p>Ngày sinh <span>(*)</span></p></td>
                <td><input type="date" name="ngaysinhtre" id="ngaysinhtre" /></td>
               <script>
  					$(function() {
  						$( "#ngaysinhtre" ).datepicker();
  					});
  				</script>
            </tr>
            <tr>
            	<td><p>Giới tính  <span>(*)</span></p></td>
                    <td><select name="gioitinhtre">
                    		<option value='Nam'>Nam</option>
							<option value='Nữ'>Nữ</option> 
  						</select>
					</td>
                </tr>
            <tr>
             	<td ><p>Dân tộc <span>(*)</span></p></td>
               	<td><select name="dantoctre">
                    	<?php 
							$result1 = $mysqli->query('select * from dantoc');
							while ($row1 = $result1->fetch_array()) {
								if ($row1['MA_DT'] == 23)
									echo "<option value='{$row1['MA_DT']}' selected>{$row1['TEN_DT']}</option>";
								else echo "<option value='{$row1['MA_DT']}'>{$row1['TEN_DT']}</option>";
							}
						?>	 
  					</select>
				</td>
            </tr>
            <tr>
             	<td ><p>Tôn giáo <span>(*)</span></p></td>
               	<td><select name="tongiaotre">
                    	<?php 
							$result1 = $mysqli->query('select * from tongiao');
							while ($row1 = $result1->fetch_array()) {
								if ($row1['MA_TG'] == 8)
									echo "<option value='{$row1['MA_TG']}' selected>{$row1['TEN_TG']}</option>";
								else echo "<option value='{$row1['MA_TG']}'>{$row1['TEN_TG']}</option>";
							}
						?>	 
  					</select>
				</td>
            </tr>
            <tr>
             	<td ><p>Phụ huynh <span>(*)</span></p></td>
               	<td><select name="phuhuynhtre">
                    	<?php 
							$result1 = $mysqli->query('select * from phuhuynh order by HOTEN_PH');
							while ($row1 = $result1->fetch_array())
								echo "<option value='{$row1['MA_PH']}'>{$row1['HOTEN_PH']}</option>";
						?>	 
  					</select>
                    <br/><button data-toggle="modal" data-target="#myModalnew" style="color:#060;font-weight:bold;font-size:13pt;margin-top:5px;background-color:#fff;">Thêm phụ huynh mới</button>
				</td>
            </tr>
            <tr>
             	<td ><p>Lớp học <span>(*)</span></p></td>
               	<td><select name="loailop" onChange="laylop()">
                    	<?php 
							$result1 = $mysqli->query('select * from loailop');
							while ($row1 = $result1->fetch_array())
								echo "<option value='{$row1['MA_LOAILOP']}'>{$row1['TEN_LOAILOP']}</option>";
						?>	 
  					</select> - 
                    <select name="lophoc">
                    	<?php
							$result1 = $mysqli->query('select * from lop where MA_LOAILOP = 1');
							while ($row1 = $result1->fetch_array())
								echo "<option value='{$row1['MA_LOP']}'>{$row1['TEN_LOP']}</option>";
						?>	 
  					</select>
				</td>
            </tr>
            <tr>
            	<td>&nbsp;</td>
                <td id="nut"><input type="submit" name="luu" value="Lưu lại"/>
                			<input type="reset" value="Nhập lại" /></td>
            </tr>
            <tr>
            	<td id="tt" colspan="2"><span>(*)</span> Thông tin bắt buộc nhập</td>
            </tr>
    	</table>
	</form>
    <script>
		function createXHR() {
			var xmlhttp;
			if (window.XMLHttpRequest) {
				xmlhttp = new XMLHttpRequest();
			} else {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return xmlhttp;
		}
		function laylop() {
			var a = document.formnew.loailop.value;
			var xhr = createXHR();
			xhr.open("get","laytenlop.php?maloai=" + a,false);
			xhr.send(null);
			document.formnew.lophoc.innerHTML = xhr.responseText;
		}
	</script>
    <script type="text/javascript">
		$("#formnew").validate({
                rules:{
                    hotentre:{
                        required:true,   
                    },
                    ngaysinhtre:{
                        required:true,
                    },
                },
                messages:{
                    hotentre:{
                        required:"Họ tên không được để trống",
                    },
                    ngaysinhtre:{
                        required:"Ngày sinh không được để trống",
                    },
                }
            });
	</script>
</div>
</div>
<div class="modal" id="myModalnew" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
	<div class="modal-content">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
		<h3 class="modal-title" id="myModalLabel" style="color:#060"><b>Thêm phụ huynh mới</b></h3>
    </div>
    	<?php
			$rs = $mysqli->query("select MAX(MA_PH) as max_ph from phuhuynh");
			$rw = $rs->fetch_array();
			$maph = $rw[0];
			$maph = "PH".str_replace(".","",number_format(((int) ltrim($rw[0],"PH") + 1)/100,2));
		?>
        <div id="insert">
		<form name="formnew2" id="formnew2" method="post">
    	<table border="0" id="bang" class="chu">
        	<tr>
            	<td width="40%"><p>Mã phụ huynh</p></td>
                <td width="60%">
                    <input type="text" name="maph" id="maph" size="10" value="<?php echo $maph; ?>" readonly />
                </td>
            </tr>
            <tr>
            	<td><p>Họ tên phụ huynh <span>(*)</span></p></td>
                <td>
                    <input type="text" name="hotenph" id="hotenph" size="40" />
                </td>
            </tr>
            <tr>
            	<td ><p>Năm sinh</p></td>
                <td><select name="namsinh">
                	<option value="0">Năm</option>
                	<?php
						for ($i = date("Y") - 70; $i <= date("Y") - 15; $i++)
							echo "<option value='$i'>$i</option>";
					?>
                    </select></td>
            </tr>
            <tr>
            	<td ><p>Nghề nghiệp</p></td>
                <td><input type="text" name="nghenghiep" id="nghenghiep" size="30" /></td>
            </tr>
            <tr>
            	<td ><p>Địa chỉ <span>(*)</span></p></td>
                <td><textarea cols="45" rows="3" name="diachiph" id="diachiph"></textarea></td>
            </tr>
             <tr>
            	<td ><p>Số điện thoại</p></td>
                <td><input type="text" name="sdt" id="sdt" size="20" onkeyup="this.value=this.value.replace(/[^\d]/,'')" /></td>
            </tr>
            <tr>
            	<td ><p>Mật khẩu <span>(*)</span></p></td>
                <td><input type="password" name="passwd" id="passwd" size="30" /></td>
            </tr>
            <tr>
            	<td>&nbsp;</td>
                <td id="nut">
                <input type="button" value="Lưu lại" onClick="themph(); return kiemtratt()" style="height:25px;width:100px"/>
                			<input type="reset" value="Nhập lại" /></td>
            </tr>
            <tr>
            	<td id="tt" colspan="2"><span>(*)</span> Thông tin bắt buộc nhập</td>
            </tr>
    	</table>
    </form>
     <script type="text/javascript">
	 	function kiemtratt() {
			var hotenph = document.formnew2.hotenph.value;
			var diachiph = document.formnew2.diachiph.value;
			var pass = document.formnew2.passwd.value;
            if (hotenph == '' || hotenph == null) {
				alert("Họ tên không được để trống");
				formnew2.hotenph.focus();
				return false;
			}
			if (diachiph == '' || diachiph == null) {
				alert("Địa chỉ không được để trống");
				formnew2.diachiph.focus();
				return false;
			}
			if (pass == '' || pass == null) {
				alert("Mật khẩu không được để trống");
				formnew2.passwd.focus();
				return false;
			}
			return true;
		}
		function createXHR() {
			var xmlhttp;
			if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
			} else {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			return xmlhttp;
		}
		function themph(){
			var maph = document.formnew2.maph.value;
			var htph = document.formnew2.hotenph.value;
			var nsph = document.formnew2.namsinh.value;
			var nnph = document.formnew2.nghenghiep.value;
			var dcph = document.formnew2.diachiph.value;
			var sdtph = document.formnew2.sdt.value;
			var pw = document.formnew2.passwd.value;
			if (htph != '' && htph != null && dcph != '' && dcph != null && pw != '' && pw != null) {
				var xhr = createXHR();
				xhr.open("get","them_ph.php?maph=" + maph + "&hotenph=" + htph + "&namsinh=" + nsph + "&nghenghiep=" + nnph + "&diachiph=" + dcph + "&sdt=" + sdtph + "&passwd=" + pw,false);
				xhr.send(null);
				document.getElementById('insert').innerHTML = xhr.responseText;
				xhr.open("get","layph.php",false);
				xhr.send(null);
				document.formnew.phuhuynhtre.innerHTML = xhr.responseText;
			}
		}
	</script>
    </div>
    </div>
    </div>
</div>
<?php
	if (isset($_REQUEST['luu'])) {
		if (isset($_REQUEST['matre']) && isset($_REQUEST['hotentre']) && isset($_REQUEST['ngaysinhtre']) 
			&& isset($_REQUEST['gioitinhtre']) && isset($_REQUEST['dantoctre']) 
			&& isset($_REQUEST['tongiaotre']) && isset($_REQUEST['phuhuynhtre']) && isset($_REQUEST['lophoc'])) {
			$insert1 = $mysqli->query("insert into tre values ('{$_REQUEST['matre']}',{$_REQUEST['dantoctre']}, 
											'{$_REQUEST['phuhuynhtre']}', {$_REQUEST['tongiaotre']}, 
											'{$_REQUEST['hotentre']}', '{$_REQUEST['ngaysinhtre']}', 
											'{$_REQUEST['gioitinhtre']}')");
			$insert2 = $mysqli->query("insert into hoc values (".date('Y').", 
									{$_REQUEST['lophoc']}, '{$_REQUEST['matre']}')");
			if ($insert1) echo "<script>alert('Thêm trẻ thành công');</script>
								<meta http-equiv='refresh' content='0;index.php?cn=ql_tre&id=ds_tre'>";
			else echo "<script>alert('Có lỗi xảy ra trong quá trình lưu thông tin trẻ');</script>";
			if (!$insert2) echo "<script>alert('Có lỗi xảy ra trong quá trình lưu thông tin lớp học của trẻ');</script>";
		}
	}
?>